﻿using KINLO.Model;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace KINLO.Data.Reporitory
{
    public class AfterWeightReporitory
    {
        private static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(WeightReporitory));
        Database DB;
        public AfterWeightReporitory()
        {
            DB = (new DbContext("name=WeightDB")).Database;
        }

        public AfterWeight GetAfterWeightByPLotNo(string lotNo)
        {
            try
            {
                var cmd = @"SELECT SystemAutoID ,Eno ,PLotNo ,PreTime ,PreWeight ,AftTime ,
                                  AftWeight ,LiquidWeight ,AddLiquidWeight ,Result ,LimitsMin ,LimitsMax 
                           FROM dbo.AfterWeight	WHERE PLotNo=@PLotNo";
                return DB.SqlQuery<AfterWeight>(cmd, 
                    new SqlParameter("@PLotNo", lotNo)).FirstOrDefault();
            }
            catch (Exception ex)
            {
                log.Error(string.Format("{0} {1} {2}", "AfterWeightReporitory", "GetAfterWeightByPLotNo", ex));
                return null;
            }
        }

        public int AddAfterWeight(AfterWeight w)
        {
            try
            {
                var cmd = @"INSERT dbo.AfterWeight
                                   ( Eno ,PLotNo ,PreTime ,PreWeight ,AftTime ,AftWeight ,
                                     LiquidWeight ,AddLiquidWeight ,Result ,LimitsMin ,LimitsMax)
                           VALUES  (@Eno  ,@PLotNo,@PreTime,@PreWeight,@AftTime ,@AftWeight ,@LiquidWeight ,@AddLiquidWeight , 
                                     @Result ,@LimitsMin,@LimitsMax)";
                return DB.ExecuteSqlCommand(cmd,
                     new SqlParameter("@Eno", w.Eno == null ? DBNull.Value : (object)w.Eno),
                     new SqlParameter("@PLotNo", w.PLotNo == null ? DBNull.Value : (object)w.PLotNo),
                     new SqlParameter("@PreTime", w.PreTime == null ? DBNull.Value : (object)w.PreTime),
                     new SqlParameter("@PreWeight", w.PreWeight == null ? DBNull.Value : (object)w.PreWeight),
                     new SqlParameter("@Result", w.Result == null ? DBNull.Value : (object)w.Result),
                     new SqlParameter("@LimitsMin", w.LimitsMin == null ? DBNull.Value : (object)w.LimitsMin),
                     new SqlParameter("@LimitsMax", w.LimitsMax == null ? DBNull.Value : (object)w.LimitsMax),
                     new SqlParameter("@AftTime", w.AftTime == null ? DBNull.Value : (object)w.AftTime),
                     new SqlParameter("@AftWeight", w.AftWeight == null ? DBNull.Value : (object)w.AftWeight),
                     new SqlParameter("@LiquidWeight", w.LiquidWeight == null ? DBNull.Value : (object)w.LiquidWeight),
                     new SqlParameter("@AddLiquidWeight", w.AddLiquidWeight == null ? DBNull.Value : (object)w.AddLiquidWeight)
                     );
            }
            catch (Exception ex)
            {
                log.Error(string.Format("{0} {1} {2}", "AfterWeightReporitory", "AddFrontWeight", ex));
                return 0;
            }
        }

        public int UpdateAfterWeight(AfterWeight w)
        {
            try
            {
                var cmd = @"UPDATE AfterWeight
                           SET Eno=@Eno ,PLotNo=@PLotNo ,PreTime=@PreTime ,PreWeight=@PreWeight ,AftTime=@AftTime ,AftWeight=@AftWeight ,
                                     LiquidWeight=@LiquidWeight ,AddLiquidWeight=@AddLiquidWeight ,Result=@Result ,LimitsMin=@LimitsMin ,LimitsMax=@LimitsMax 
                           WHERE SystemAutoID=@SystemAutoID";
                return DB.ExecuteSqlCommand(cmd,
                     new SqlParameter("@Eno", w.Eno == null ? DBNull.Value : (object)w.Eno),
                     new SqlParameter("@PLotNo", w.PLotNo == null ? DBNull.Value : (object)w.PLotNo),
                     new SqlParameter("@PreTime", w.PreTime == null ? DBNull.Value : (object)w.PreTime),
                     new SqlParameter("@PreWeight", w.PreWeight == null ? DBNull.Value : (object)w.PreWeight),
                     new SqlParameter("@Result", w.Result == null ? DBNull.Value : (object)w.Result),
                     new SqlParameter("@LimitsMin", w.LimitsMin == null ? DBNull.Value : (object)w.LimitsMin),
                     new SqlParameter("@LimitsMax", w.LimitsMax == null ? DBNull.Value : (object)w.LimitsMax),
                     new SqlParameter("@AftTime", w.AftTime == null ? DBNull.Value : (object)w.AftTime),
                     new SqlParameter("@AftWeight", w.AftWeight == null ? DBNull.Value : (object)w.AftWeight),
                     new SqlParameter("@LiquidWeight", w.LiquidWeight == null ? DBNull.Value : (object)w.LiquidWeight),
                     new SqlParameter("@AddLiquidWeight", w.AddLiquidWeight == null ? DBNull.Value : (object)w.AddLiquidWeight),
                     new SqlParameter("@SystemAutoID", w.SystemAutoID)
                     );
            }
            catch (Exception ex)
            {
                log.Error(string.Format("{0} {1} {2}", "AfterWeightReporitory", "UpdateFrontWeight", ex));
                return 0;
            }
        }

        public int DeleteAfterWeight(string plotNo )
        {
            try
            {
                var cmd = @"DELETE AfterWeight WHERE PLotNo=@PLotNo";
                return DB.ExecuteSqlCommand(cmd,
                      new SqlParameter("@PLotNo", plotNo));
            }
            catch (Exception ex)
            {
                log.Error(string.Format("{0} {1} {2}", "AfterWeightReporitory", "DeleteFrontWeight", ex));
                return 0;
            }

        }

    }
}
